package com.deer.wms.quartz.task;

import com.deer.wms.ASN.manage.model.AcceptEx;
import com.deer.wms.ASN.manage.model.AcceptLog;
import com.deer.wms.ASN.manage.model.SjcUpperTask;
import com.deer.wms.ASN.manage.model.SjcUpperTaskCriteria;
import com.deer.wms.ASN.manage.model.in.AcceptRecord;
import com.deer.wms.ASN.manage.model.in.AcceptRecordCriteria;
import com.deer.wms.ASN.manage.service.AcceptExService;
import com.deer.wms.ASN.manage.service.AcceptLogService;
import com.deer.wms.ASN.manage.service.AcceptRecordService;
import com.deer.wms.ASN.manage.service.SjcUpperTaskService;
import com.deer.wms.project.root.util.DateUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Component("acceptLogTask")
public class AcceptLogTask {

    @Resource
    private AcceptLogService acceptLogService;

    @Resource
    private AcceptRecordService acceptRecordService;

    @Resource
    private SjcUpperTaskService sjcUpperTaskService;

    @Resource
    private AcceptExService acceptExService;

    public void createAcceptLog() {
        Date now = DateUtils.now();
        // 如果时间为空创建全部，不为空，查询大于最大时间的验收单
        Date maxLogDate = acceptLogService.getMaxLogDate();
        AcceptRecordCriteria acceptRecordCriteria = new AcceptRecordCriteria();
        acceptRecordCriteria.setGtCreateTime(DateUtils.dateToStr(maxLogDate, DateUtils.DEFAULT_DATETIME_FORMAT));
        List<AcceptRecord> acceptRecordList = acceptRecordService.findList(acceptRecordCriteria);
        if (CollectionUtils.isEmpty(acceptRecordList)) {
            return;
        }
        List<AcceptLog> acceptLogList = new ArrayList<>();
        for (AcceptRecord acceptRecord : acceptRecordList) {
            AcceptLog acceptLog = new AcceptLog();
            acceptLog.setWareId(acceptRecord.getWareId());
            acceptLog.setWareName(acceptRecord.getWareName());
            acceptLog.setItemCode(acceptRecord.getItemCode());
            acceptLog.setItemName(acceptRecord.getItemName());
            acceptLog.setItemOriginalName(null);
            acceptLog.setImgUrl(acceptRecord.getImgUrl());
            acceptLog.setSpec(acceptRecord.getSpec());
            acceptLog.setModel(acceptRecord.getModel());
            acceptLog.setBatchId(acceptRecord.getBatchId());
            acceptLog.setBatchName(acceptRecord.getBatchName());
            acceptLog.setCellCode(acceptRecord.getCellCode());
            acceptLog.setBillType(acceptRecord.getBillType());
            acceptLog.setAcceptType(1);
            acceptLog.setQcType(1);
            acceptLog.setAcceptState(acceptRecord.getState());
            acceptLog.setAcceptUserId(acceptRecord.getAcceptUserId());
            acceptLog.setAcceptUserName(acceptRecord.getAcceptUserName());
            acceptLog.setAcceptTime(DateUtils.strToDate(acceptRecord.getAcceptTime(), DateUtils.DEFAULT_DATETIME_FORMAT));
            // 关联上架车信息
            if (Objects.nonNull(acceptRecord.getInventoryId())) {
                List<SjcUpperTask> sjcList;
                SjcUpperTaskCriteria sjcUpperTaskCriteria = new SjcUpperTaskCriteria();
                sjcUpperTaskCriteria.setInventoryId(acceptRecord.getInventoryId());
                sjcList = sjcUpperTaskService.findList(sjcUpperTaskCriteria);
                if (!CollectionUtils.isEmpty(sjcList)) {
                    SjcUpperTask sjcUpperTask = sjcList.get(0);
                    acceptLog.setUpUserId(sjcUpperTask.getUpUserId());
                    acceptLog.setUpUserName(sjcUpperTask.getUpUserName());
                    acceptLog.setUpTime(DateUtils.strToDate(sjcUpperTask.getUpTime(), DateUtils.DEFAULT_DATETIME_FORMAT));
                }
            } else {
                acceptLog.setAcceptType(2);
            }
            acceptLog.setExpectQuantity(acceptRecord.getExpectQuantity());
            acceptLog.setQuantity(acceptRecord.getQuantity());
            // 关联验收异常信息
            if (Objects.nonNull(acceptRecord.getAcceptExId())) {
                AcceptEx acceptEx = acceptExService.getById(acceptRecord.getAcceptExId());
                if (acceptEx != null) {
                    acceptLog.setExQuantity(acceptEx.getQuantity());
                }
            } else {
                acceptLog.setExQuantity(0.0d);
            }
            acceptLog.setBillNo(acceptRecord.getAsnBillNo());
            acceptLog.setCreateTime(now);
            acceptLogService.save(acceptLog);
//            acceptLogList.add(acceptLog);
        }
//        acceptLogService.save(acceptLogList);
    }

}
